<?php


namespace app\common\library;


use common;
use RequestContext;
use vtimer;

class TBitEcuModel
{
    static function push_server_url()
    {
        static $psu = null;
        if (empty($psu)) {
            $psu = config('app.tbit_ecu_push_server');
        }
        return $psu;
    }

    function code_to_msg($code)
    {
        $code_eror_msg = [
            1 => [ECU_ERROR_OK, 'OK'],
            0 => [ECU_ERROR_500, '失败'],
            2 => [ECU_ERROR_IN_MOTION, '运动中'],
            3 => [ECU_ERROR_OFFPOWER, '电池空置'],
            109 => [ECU_ERROR_OFFLINE, '设备离线'],
        ];

        if (isset($code_eror_msg[$code])) {
            return $code_eror_msg[$code];
        }
        return [ECU_ERROR_500, '未定义错误'];
    }

    function get_xc_access_token($imei)
    {
        return '3FE26914980F74D13102E343E22D9492';
    }

    function get_agent_id($imei)
    {
        return 263;
    }

    function lock($ecu_sn)
    {
        $payload = [
            'cmd' => 6,
            'ctrl' => 1,
        ];

        $params = [
            'cmd' => 'push_request',
            'type' => 1,
            'to_id' => $ecu_sn,
            'sn' => RequestContext::$sn,
            'payload' => json_encode($payload),
        ];
        $vt = new vtimer();
        $result = common::curlpostjson(self::push_server_url(), $params);
        if (empty($result)) {
            return [-1, "request error!", $vt->cost()];
        }

        if ($result['status'] == 0) {
            $result = json_decode($result['payload'], true);
            if ($result) {
                list($code, $msg) = $this->code_to_msg($result['code']);
                return [$code, $msg, $vt->cost()];
            }
        }
        return [-2, '请重试！', $vt->cost()];
    }

    function temp_lock($ecu_sn)
    {
        $payload = [
            'cmd' => 6,
            'ctrl' => 48,
        ];

        $params = [
            'cmd' => 'push_request',
            'type' => 1,
            'to_id' => $ecu_sn,
            'sn' => RequestContext::$sn,
            'payload' => json_encode($payload),
        ];
        $vt = new vtimer();
        $result = common::curlpostjson(self::push_server_url(), $params);
        if (empty($result)) {
            return [-1, "request error!", $vt->cost()];
        }

        if ($result['status'] == 0) {
            $result = json_decode($result['payload'], true);
            if ($result) {
                list($code, $msg) = $this->code_to_msg($result['code']);
                return [$code, $msg, $vt->cost()];
            }
        }
        return [-2, '请重试！', $vt->cost()];
    }

    function acc_off($ecu_sn)
    {
        $payload = [
            'cmd' => 6,
            'ctrl' => 27,
        ];

        $params = [
            'cmd' => 'push_request',
            'type' => 1,
            'to_id' => $ecu_sn,
            'sn' => RequestContext::$sn,
            'payload' => json_encode($payload),
        ];
        $vt = new vtimer();
        $result = common::curlpostjson(self::push_server_url(), $params);
        if (empty($result)) {
            return [-1, "request error!", $vt->cost()];
        }

        if ($result['status'] == 0) {
            $result = json_decode($result['payload'], true);
            if ($result) {
                list($code, $msg) = $this->code_to_msg($result['code']);
                return [$code, $msg, $vt->cost()];
            }
        }
        return [-2, '请重试！', $vt->cost()];
    }

    function acc_on($ecu_sn)
    {
        $payload = [
            'cmd' => 6,
            'ctrl' => 28,
        ];

        $params = [
            'cmd' => 'push_request',
            'type' => 1,
            'to_id' => $ecu_sn,
            'sn' => RequestContext::$sn,
            'payload' => json_encode($payload),
        ];
        $vt = new vtimer();
        $result = common::curlpostjson(self::push_server_url(), $params);
        if (empty($result)) {
            return [-1, "request error!", $vt->cost()];
        }

        if ($result['status'] == 0) {
            $result = json_decode($result['payload'], true);
            if ($result) {
                list($code, $msg) = $this->code_to_msg($result['code']);
                return [$code, $msg, $vt->cost()];
            }
        }
        return [-2, '请重试！', $vt->cost()];
    }

    function unlock($ecu_sn)
    {
        $payload = [
            'cmd' => 6,
            'ctrl' => 11,
        ];

        $params = [
            'cmd' => 'push_request',
            'type' => 1,
            'to_id' => $ecu_sn,
            'sn' => RequestContext::$sn,
            'payload' => json_encode($payload),
        ];
        $vt = new vtimer();
        $result = common::curlpostjson(self::push_server_url(), $params);
        if (empty($result)) {
            return [-1, "request error!", $vt->cost()];
        }

        if ($result['status'] == 0) {
            $result = json_decode($result['payload'], true);
            if ($result) {
                list($code, $msg) = $this->code_to_msg($result['code']);
                return [$code, $msg, $vt->cost()];
            }
        }
        return [-2, '请重试！', $vt->cost()];
    }


    function unlock_battery($ecu_sn)
    {
        $payload = [
            'cmd' => 6,
            'ctrl' => 15,
        ];

        $params = [
            'cmd' => 'push_request',
            'type' => 1,
            'to_id' => $ecu_sn,
            'sn' => RequestContext::$sn,
            'payload' => json_encode($payload),
        ];
        $vt = new vtimer();
        $result = common::curlpostjson(self::push_server_url(), $params);
        if (empty($result)) {
            return [-1, "request error!", $vt->cost()];
        }

        if ($result['status'] == 0) {
            $result = json_decode($result['payload'], true);
            if ($result) {
                list($code, $msg) = $this->code_to_msg($result['code']);
                return [$code, $msg, $vt->cost()];
            }
        }
        return [-2, '请重试！', $vt->cost()];
    }

    function lock_battery($ecu_sn)
    {
        $payload = [
            'cmd' => 6,
            'ctrl' => 16,
        ];

        $params = [
            'cmd' => 'push_request',
            'type' => 1,
            'to_id' => $ecu_sn,
            'sn' => RequestContext::$sn,
            'payload' => json_encode($payload),
        ];
        $vt = new vtimer();
        $result = common::curlpostjson(self::push_server_url(), $params);
        if (empty($result)) {
            return [-1, "request error!", $vt->cost()];
        }

        if ($result['status'] == 0) {
            $result = json_decode($result['payload'], true);
            if ($result) {
                list($code, $msg) = $this->code_to_msg($result['code']);
                return [$code, $msg, $vt->cost()];
            }
        }
        return [-2, '请重试！', $vt->cost()];
    }

    function locate_rt($ecu_sn)
    {
        $payload = [
            'cmd' => 6,
            'ctrl' => 8,
        ];

        $params = [
            'cmd' => 'push_request',
            'type' => 1,
            'to_id' => $ecu_sn,
            'sn' => RequestContext::$sn,
            'payload' => json_encode($payload),
        ];
        $vt = new vtimer();
        $result = common::curlpostjson_timeout(self::push_server_url(), $params,10);
        if (empty($result)) {
            return [-1, "request error!", $vt->cost()];
        }

        if ($result['status'] == 0) {
            $result = json_decode($result['payload'], true);
            if ($result) {
                list($code, $msg) = $this->code_to_msg($result['code']);
                return [$code, $msg, $vt->cost()];
            }
        }
        return [-2, '请重试！', $vt->cost()];
    }

    /*
      1	锁车成功
      2 	开锁成功
      3	开车成功
      4	锁车成功
      5	欢迎使用电单车
      6	欢迎使用电单车
      7 即将骑出服务区，车辆将自动断电
      8 你已骑出服务区，请立即返回
      9	我在这里
      10	滴滴滴滴
     */

    const TBIT_VOICE_TYPE_OSVC_ALEADY = 3;
    const TBIT_VOICE_TYPE_OSVC = 12;
    const TBIT_VOICE_TYPE_RING = 5;
    const TBIT_VOICE_TYPE_PARKWARN = 9;
    const TBIT_VOICE_TYPE_OVERSPEED = 14;

    function device_voice($ecu_sn, $type)
    {
        $payload = [
            'cmd' => 12,
            'index' => $type,
        ];

        $params = [
            'cmd' => 'push_request',
            'type' => 1,
            'to_id' => $ecu_sn,
            'sn' => RequestContext::$sn,
            'payload' => json_encode($payload),
        ];
        $vt = new vtimer();
        $result = common::curlpostjson_timeout(TBitEcuModel::push_server_url(), $params,10);
        if (empty($result)) {
            return [-1, "request error!", $vt->cost()];
        }

        if ($result['status'] == 0) {
            $result = json_decode($result['payload'], true);
            if ($result) {
                list($code, $msg) = $this->code_to_msg($result['code']);
                $code = $result['code'];
                if ($code == 0){
                    $msg = 'OK';
                }
                return [$code, $msg, $vt->cost()];
            }
        }
        return [-2, '请重试！', $vt->cost()];
    }

    //超速告警
    function move_limit($ecu_sn)
    {

        $payload = [
            'cmd' => 7,
            'cfg' => 'SPEED;',
        ];

        $params = [
            'cmd' => 'push_request',
            'type' => 1,
            'to_id' => $ecu_sn,
            'sn' => RequestContext::$sn,
            'payload' => json_encode($payload),
        ];
        $vt = new vtimer();
        $result = common::curlpostjson(TBitEcuModel::push_server_url(), $params);
        if (empty($result)) {
            return [-1, "request error!", $vt->cost()];
        }

        if ($result['status'] == 0) {
            $result = json_decode($result['payload'], true);
            if ($result) {
                list($code, $msg) = $this->code_to_msg($result['code']);
                return [$code, $msg, $vt->cost()];
            }
        }
        return [-2, '请重试！', $vt->cost()];
    }

    //查询
    function get_cfg($ecu_sn,$cfg){
        $payload = [
            'cmd' => 7,
            'cfg' => $cfg,
        ];

        $params = [
            'cmd' => 'push_request',
            'type' => 1,
            'to_id' => $ecu_sn,
            'sn' => RequestContext::$sn,
            'payload' => json_encode($payload),
        ];
        $vt = new vtimer();
        $result = common::curlpostjson_timeout(TBitEcuModel::push_server_url(), $params,30);
        if (empty($result)) {
            return [-1, "request error!", $vt->cost()];
        }

        if ($result['status'] == 0) {
            $result = json_decode($result['payload'], true);
            if ($result) {
                list($code, $msg) = $this->code_to_msg($result['code']);
                return [$code, $msg, $vt->cost(),$result];
            }
        }
        return [-2, '请重试！', $vt->cost(),""];
    }

    function set_cfg($ecu_sn,$cfg){
        $payload = [
            'cmd' => 8,
            'cfg' => $cfg,
        ];

        $params = [
            'cmd' => 'push_request',
            'type' => 1,
            'to_id' => $ecu_sn,
            'sn' => RequestContext::$sn,
            'payload' => json_encode($payload),
        ];
        $vt = new vtimer();
        $result = common::curlpostjson_timeout(TBitEcuModel::push_server_url(), $params,30);
        if (empty($result)) {
            return [-1, "request error!", $vt->cost()];
        }

        if ($result['status'] == 0) {
            $result = json_decode($result['payload'], true);
            if ($result) {
                list($code, $msg) = $this->code_to_msg($result['code']);
                $code = $result['code'];
                if ($code == 0){
                    $msg = 'OK';
                }
                return [$code, $msg, $vt->cost(),$result];
            }
        }
        return [-2, '请重试！', $vt->cost(),""];
    }
}